<template>
  <a-drawer :title="title" width="40%" :visible="visible" :confirmLoading="loading" @close="() => {
      this.visible = false
    }
    ">
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="主键" prop="Id" v-show="false">
          <a-input v-model="entity.Id" autocomplete="off" placeholder="主键" />
        </a-form-model-item>
        <a-form-model-item label="货物编码" prop="code">
          <a-input v-model="entity.code" autocomplete="off" placeholder="货物编码" />
        </a-form-model-item>
        <a-form-model-item label="货物名称" prop="name">
          <a-input v-model="entity.name" autocomplete="off" placeholder="货物名称" />
        </a-form-model-item>
        <a-form-model-item label="规格" prop="specification">
          <a-input v-model="entity.specification" autocomplete="off" placeholder="规格" />
        </a-form-model-item>
        <!-- <a-form-model-item label="单价" prop="TaxUnitPrice">
          <a-input v-model="entity.TaxUnitPrice" autocomplete="off" placeholder="单价" />
        </a-form-model-item>
        <a-form-model-item label="是否含税" prop="IsTax">
          <a-switch v-model="entity.IsTax" :checked="entity.TM_IsTax" />
        </a-form-model-item>
        <a-form-model-item label="税率" prop="TaxRate">
          <a-input v-model="entity.TaxRate" autocomplete="off" placeholder="税率" />
        </a-form-model-item> -->
      </a-form-model>
    </a-spin>
    <div :style="{
      position: 'absolute',
      right: 0,
      bottom: 0,
      width: '100%',
      borderTop: '1px solid #e9e9e9',
      padding: '4px 16px',
      background: '#fff',
      textAlign: 'right',
      zIndex: 1
    }">
      <a-button type="primary" @click="handleSave">确定</a-button>&nbsp;
      <a-button @click="close">取消</a-button>&nbsp;
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {
        code: [
          { required: true, message: '请填写税务编码', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请填写物料简码', trigger: 'blur' },
        ],
        specification: [
          { required: true, message: '请填写物料名称', trigger: 'blur' },
        ],
      },
      title: ''
    }
  },
  methods: {
    handleSave() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.entity.TM_IsTax) { this.entity.TM_IsTax = 1 } else { this.entity.TM_IsTax = 0 }
          this.$http.post('/MaterialManagement/AA_Inventory/SaveData', this.entity).then(res => {
            if (res.Success) {
              this.$message.success('保存成功!')
              this.visible = false
              this.parentObj.getDataList();
            }
          }).catch(err => {
            this.$message.error(err)
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
      
    },
    close() {
      this.visible = false
      this.entity = {}
    },
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/MaterialManagement/AA_Inventory/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/OrderManagement/Ap_CloseBill/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
